package qianfeng.com.fan_shu_xiao_bao.view;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

import qianfeng.com.fan_shu_xiao_bao.R;
import qianfeng.com.fan_shu_xiao_bao.view.base.Base_RecyclerView_Adapter;

/**
 * Created by Li_ke on 2017/1/17.
 */
public class TopTab_RecyclerView_Adapter extends Base_RecyclerView_Adapter<String, TopTab_RecyclerView_Adapter.ViewHolder> {

    //当前选中的标签
    int selected;

    //两种类型:选中与未选中
    final int TYPE_NO_SELECTED = 0;
    final int TYPE_SELECTED = 1;

    public TopTab_RecyclerView_Adapter(Context context, List<String> datas) {
        super(context, datas);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ViewHolder holder = null;
        switch (viewType) {
            case TYPE_NO_SELECTED:
                View view = LayoutInflater.from(context).inflate(R.layout.item_toptab_selected, parent, false);
                holder = new ViewHolder(view);
                holder.title = ((TextView) view.findViewById(R.id.item_toptab_selected_title));
                break;

            case TYPE_SELECTED:
                View view1 = LayoutInflater.from(context).inflate(R.layout.item_toptab_no_selected, parent, false);
                holder = new ViewHolder(view1);
                holder.title = ((TextView) view1.findViewById(R.id.item_toptab_no_selected_title));
                break;
        }
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.title.setText(datas.get(position));
    }

    @Override
    public int getItemViewType(int position) {
        return position == selected ? TYPE_SELECTED : TYPE_NO_SELECTED;
    }

    class ViewHolder extends Base_RecyclerView_Adapter.ViewHolder {
        TextView title;

        public ViewHolder(View itemView) {
            super(itemView);
        }

        @Override
        public void onClick(View v) {
            selected = getAdapterPosition();
            super.onClick(v);
            notifyDataSetChanged();
        }
    }
}
